package 笔试;

import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {6, 3, 8, 2, 9, 1};
        System.out.println("排序前数组为：");
        for (int i : arr) {
            System.out.print(i);
        }
        sort1(arr);
        System.out.println();
        System.out.println("排序前数组后：");
        for (int i : arr) {
            System.out.print(i);
        }
    }

    private static void sort(int[] arr) {
        boolean flag = false;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    flag = true;
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
//                System.out.println("第" + (i + 1) + "趟后的数组元素");
//                System.out.println(Arrays.toString(arr));
            }
            if (!flag) {
                break;
            } else {
                flag = false;
            }
        }
    }

    private static void sort1(int[] arr) {
        int temp = 0;//用于交换的数据
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}
